﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FightTheDragon.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;




namespace FightTheDragon.GameObjects
{
    class Player : BaseObject
    {
        private static Player m_Instance = new Player();

        public static Player Instance
        {
            get { return m_Instance; }
        }
        
        private int m_nScore;
        public int Score
        {
            get { return m_nScore; }
            set { m_nScore = value; }
        }

        private bool m_bJumping;
        public bool BJumping
        {
            get { return m_bJumping; }
            set { m_bJumping = value; }
        }
       
        // Player only has a default Constructer as its position will be preDefined
        public Player()
        {

            Texture = "FtD_Character";//Change Once we get Done

            NObjectType = Types.PLAYER;
            FScale = 1.0f;
            Width = GlobalContentManager.Instance.Texture( Texture ).Width ;
            Height = GlobalContentManager.Instance.Texture( Texture ).Height ;
            
            SpriteColor = Color.White;
            VPosition = new Vector2(50, 685 - ((int)Height >> 1));
            VVelocity = new Vector2(0, 0);
            Origin = new Vector2(((int)Width >> 1), ((int)Height >> 1));
            FRotation = 0;
            m_nScore = 0;
           
        }
        public override void Update(float fgameTime)
        {
            base.Update(fgameTime);
            HandleMovement(fgameTime);


        }
        public void HandleMovement(float gTime)
        {
            InputManager Input = InputManager.Instance;
            Vector2 newPos;
            if (Input.KeyDown(Keys.Left))
            {
                newPos = VVelocity;
                newPos.X = -gTime * 0.2f;
                VVelocity = newPos;
            }
            else if (Input.KeyDown(Keys.Right))
            {
                newPos = VVelocity;
                newPos.X = gTime * 0.2f;
                VVelocity = newPos;
            }
            else
            {
                newPos = VVelocity;
                newPos.X = 0;
                VVelocity = newPos;
            }
            if ( Input.KeyPressedOnce(Keys.Space) && m_bJumping == false )
            {
                newPos = VVelocity;
                newPos.Y = -10.0f;
                VVelocity = newPos;
                m_bJumping = true;
            }
            if (m_bJumping)
            {
                newPos = VVelocity;
                newPos.Y += gTime * 0.02f;
                VVelocity = newPos;
                if (VPosition.Y > 685 - ((int)Height >> 1))
                {
                    newPos = VPosition;
                    newPos.Y = 685 - ((int)Height >> 1 );
                    VPosition = newPos;
                    newPos = VVelocity;
                    newPos.Y = 0;
                    VVelocity = newPos;
                    m_bJumping = false;
                }
            }
        }



        
    }
}
